﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using Hematology.Data;
using Hematology.HmtMembership;
using Hematology.Tools;
using Hematology.UserSite.Controls;
using NgoResource = Hematology.UserSite.App_GlobalResources.UserSite;

namespace Hematology.UserSite
{
	public partial class Events : NgoPage
	{
		#region Properties

		protected EnumHelper.EventType CurrentEventType
		{
			get
			{
				return ViewState["CurrentEventType"] != null
						   ? (EnumHelper.EventType)Enum.Parse(typeof(EnumHelper.EventType), ViewState["CurrentEventType"].ToString())
						   : EnumHelper.EventType.Nothing;
			}
			set { ViewState["CurrentEventType"] = value; }
		}

		#endregion

		#region Fill methods

		private void FillSideLinks()
		{
			List<SiteEvent> lSiteEvents = SiteEvent.ListAllEvents(null, !MembershipHelper.MembershipService.IsAuthorizedRequest);
			if (lSiteEvents != null)
			{
				hlCongress.Text = String.Format("{0} [{1}]", NgoResource.resTopicCongress,
				                                lSiteEvents.Where(
				                                	siteEvent => siteEvent.EventTypeCode == (int) EnumHelper.EventType.Congress).Count());

				hlConference.Text = String.Format("{0} [{1}]", NgoResource.resTopicConference,
				                                  lSiteEvents.Where(
													siteEvent => siteEvent.EventTypeCode == (int)EnumHelper.EventType.Conference).Count());

				hlNosology.Text = String.Format("{0} [{1}]", NgoResource.resTopicNosology,
				                                lSiteEvents.Where(
													siteEvent => siteEvent.EventTypeCode == (int)EnumHelper.EventType.Nosology).Count());

				hlSeminar.Text = String.Format("{0} [{1}]", NgoResource.resTopicSeminar,
				                               lSiteEvents.Where(
												siteEvent => siteEvent.EventTypeCode == (int)EnumHelper.EventType.Seminar).Count());
			}
		}

		private void FillViewStateVariables()
		{
			string eventType = Request.QueryString["et"];
			if (Array.IndexOf(Enum.GetNames(typeof(EnumHelper.EventType)), eventType) > -1)
			{
				CurrentEventType =
					(EnumHelper.EventType)Enum.Parse(typeof(EnumHelper.EventType), eventType);
			}
		}

		private void FillSummaryInfo()
		{
			List<SiteEvent> lEvents = new List<SiteEvent>
			                          	{
			                          		GetLastEvent(EnumHelper.EventType.Congress),
			                          		GetLastEvent(EnumHelper.EventType.Conference),
			                          		GetLastEvent(EnumHelper.EventType.Seminar),
			                          		GetLastEvent(EnumHelper.EventType.Nosology)
			                          	};

			lEvents.RemoveAll(siteEvent => siteEvent == null);

			rptEventsFull.DataSource = lEvents;
			rptEventsFull.DataBind();
		}

		private void FillEventInfoList(int iContentTypeCode, Repeater rptTargetRepeater)
		{
			List<SiteEvent> lEvents = SiteEvent.ListAllEvents(iContentTypeCode, !MembershipHelper.MembershipService.IsAuthorizedRequest);

			rptTargetRepeater.DataSource = lEvents;
			rptTargetRepeater.DataBind();
		}

		private void FillEventList()
		{
			if (CurrentEventType == EnumHelper.EventType.Nothing)
			{
				FillSummaryInfo();
			}
			else
			{
				FillEventInfoList((int)CurrentEventType, rptEventsFull);
			}
		}

		private void FillEventProperties()
		{
			if (CurrentEventType == EnumHelper.EventType.Nothing)
			{
				lblEventListType.Text = NgoResource.resTopicEvents;
				ptTitle.Text = NgoResource.resTopicEvents;

				mvEventType.ActiveViewIndex = 0;
			}
			else
			{
				int iCurrentEventType = (int)CurrentEventType;

				SiteEventType siteEventType = SiteEventType.GetSiteEventTypeByCode(iCurrentEventType);
				if (siteEventType != null)
				{
					lblEventListType.Text = siteEventType.Name;
					ptTitle.Text = siteEventType.Name;
				}

				FillEventInfoList(iCurrentEventType, rptEvents);

				mvEventType.ActiveViewIndex = 1;
			}
		}

		private void FillEventInfo()
		{
			string sSiteEventId = Request.QueryString["id"];
			int iSiteEventId;
			if (!String.IsNullOrEmpty(sSiteEventId) && Int32.TryParse(sSiteEventId, out iSiteEventId))
			{
				ptTitle.NavigateUrl = String.Format("/Events.aspx?et={0}", CurrentEventType);
				SiteEvent oSiteEvent = SiteEvent.Get(iSiteEventId);
				if (oSiteEvent != null)
				{
					List<SiteEvent> oSiteEventList = new List<SiteEvent>
                                                                 {
                                                                     oSiteEvent
                                                                 };

					fvEvent.DataSource = oSiteEventList;
					fvEvent.DataBind();
				}
			}
			else
			{
				if (CurrentEventType != EnumHelper.EventType.Nothing)
					ptTitle.NavigateUrl = "/Events.aspx";
				FillEventList();
			}
		}

		#endregion

		#region Other methods

		private SiteEvent GetLastEvent(EnumHelper.EventType eventType)
		{
			return SiteEvent.ListAllEvents((int)eventType, !MembershipHelper.MembershipService.IsAuthorizedRequest).FirstOrDefault();
		}

		protected bool GetSubmitButtonVisibility(object oFinishDate)
		{
			bool bIsVisible = false;

			if (MembershipHelper.MembershipService.IsAuthorizedRequest && oFinishDate != null)
			{
				DateTime dtFinishDate = Convert.ToDateTime(oFinishDate);

				bIsVisible = dtFinishDate > DateTime.Now;
			}
			return bIsVisible;
		}

		#endregion

		#region Event handlers

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				FillViewStateVariables();
				FillEventProperties();
				FillEventInfo();
				FillSideLinks();
			}
		}

		protected void btnSubmitForEvent_Click(object sender, EventArgs e)
		{

		}

		protected void rptContentAttach_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "OpenAttach")
			{
				int iAttachId = Convert.ToInt32(e.CommandArgument);
				FileAttach fileAttach = FileAttach.Get(iAttachId);
				if (fileAttach != null)
				{
					Response.Redirect(Path.Combine(ConfigReader.EventAttachmentPath, fileAttach.DocumentName));
				}
			}
		}

		#endregion
	}
}